package com.biscuit.page.basicservice.design.login.strategy;

import com.alibaba.fastjson.JSON;
import com.biscuit.page.basicservice.api.model.request.authorization.AuthorizationAReq;
import com.biscuit.page.basicservice.design.login.LoginType;
import com.biscuit.page.basicservice.model.entities.mysql.SysUser;
import com.biscuit.page.basicservice.model.enums.LoginTypeEnum;
import com.biscuit.page.basicservice.model.enums.VerifyCodeWorkTypeEnum;
import com.biscuit.page.basicservice.persistence.dao.UserDao;
import com.biscuit.page.basicservice.persistence.dao.UserDetailDao;
import com.biscuit.page.basicservice.service.CacheService;
import com.biscuit.page.basicservice.service.CommonService;
import com.biscuit.page.basicservice.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 注册登录
 *
 * @author biscuit
 * @createTime 2024/5/22 13:54
 */
@Slf4j
@Component
@LoginType(type = LoginTypeEnum.REGISTER_LOGIN)
public class RegisterLogin implements LoginMoreTypeInterface {

    @Resource
    private UserDao userDao;

    @Resource
    private UserDetailDao userDetailDao;

    @Resource
    private CommonService commonService;

    @Override
    public SysUser login(AuthorizationAReq req) {
        log.info("AuthorizationService login for RegisterLogin start，request -> {}", JSON.toJSONString(req));

        // 参数校验
        BaseExceptionUtil.throwException(req.getPassword().length() > 5, "密码长度不能小于6");
        BaseExceptionUtil.throwException(StringUtils.isNotBlank(req.getPassword()) && StringUtils.isNotBlank(req.getVerifyPassword()), "密码不能为空");
        BaseExceptionUtil.throwException(StringUtils.isNotBlank(req.getVerifyCode()), "验证码不能为空！");

        // 校验验证码
        this.commonService.checkVerifyCode(VerifyCodeWorkTypeEnum.REGISTER_VERIFY_CODE.getType(), req.getAccount(), req.getVerifyCode());

        // 根据账号获取用户信息
        SysUser sysUser = this.userDao.getByAccount(req.getAccount());

        // 判断用户是否已经存在
        BaseExceptionUtil.throwException(Objects.isNull(sysUser), "用户信息已存在");

        // 密码非对称解密
        String decryptPassword = SecurityUtil.decrypt(req.getPassword());
        String decryptVerifyPassword = SecurityUtil.decrypt(req.getVerifyPassword());

        // 校验两次密码是否相同
        BaseExceptionUtil.throwException(decryptPassword.equals(decryptVerifyPassword), "两次输入密码不一致！");

        // 用户信息初始化
        sysUser = new SysUser();
        sysUser.setUId(CommonUtil.getUUID());
        sysUser.setNickName(TextUtil.getRandomNickname());
        // account.setAvatar("default.jpg");
        sysUser.setStatus(1);
        sysUser.setPassword(MD5Util.md5(decryptPassword));
        sysUser.setBirthday(DateUtil.getLocalDate("1900-01-01"));
        if (req.getAccountType() == 1) {
            sysUser.setEmail(req.getAccount());
        }
        if (req.getAccountType() == 2) {
            sysUser.setMobile(req.getAccount());
        }

        // 保存用户信息
        this.userDao.insertAndIncrement(sysUser);

        // 初始化用户详情信息
        this.userDetailDao.initUserDetail(sysUser.getId());

        log.info("AuthorizationService login for RegisterLogin end，response -> {}", JSON.toJSONString(sysUser));
        return sysUser;
    }

}
